package com.definesys.smecrailtransit.SmecManager.SmecRetrofit;


import com.definesys.smecrailtransit.SmecApplication;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.Interceptor.TokenInterceptor;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.api.SmecRailService;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * Created by xupeizuo on 2017/07/20.
 */

public final class RetrofitServiceManager {

    private static final String TAG=RetrofitServiceManager.class.getSimpleName();

    //设缓存有效期为1天
    public static final long CACHE_STALE_SEC = 60 * 60 * 24 * 1;

    //查询缓存的Cache-Control设置，为if-only-cache时只查询缓存而不会请求服务器，max-stale可以配合设置缓存失效时间
    public static final String CACHE_CONTROL_CACHE = "only-if-cached, max-stale=" + CACHE_STALE_SEC;

    //查询网络的Cache-Control设置
    //(假如请求了服务器并在a时刻返回响应结果，则在max-age规定的秒数内，浏览器将不会发送对应的请求到服务器，数据由缓存直接返回)
    public static final String CACHE_CONTROL_NETWORK = "Cache-Control: public, max-age=3600";

    // 避免出现 HTTP 403 Forbidden，参考：http://stackoverflow.com/questions/13670692/403-forbidden-with-java-but-not-web-browser
    public static final String AVOID_HTTP403_FORBIDDEN = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11";

    public static final String JSON_HEAD="Accept: application/vnd.githuSampleb.v3.full+json";

    public static final String POST_HEAD="Content-Type: application/x-www-form-urlencoded; charset=utf-8";

    private RetrofitServiceManager() {
    }

    private static Retrofit mRetrofit;
    private static HttpLoggingInterceptor httpLoggingInterceptor ;
    private static SmecRailService smecRailService;

    private static byte[] syncObj = new byte[0];

    static {
        httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    }

    /**
     * 初始化网络请求
     */
    public static void init(){
        // 指定缓存路径,缓存大小100Mb
        Cache cache = new Cache(new File(SmecApplication.getContext().getCacheDir(), "HttpCache"),
                1024 * 1024 * 100);
        OkHttpClient okHttpClient = new OkHttpClient.Builder().cache(cache)
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(new TokenInterceptor())
                .addInterceptor(httpLoggingInterceptor)
                .addNetworkInterceptor(httpLoggingInterceptor)
                .build();

        mRetrofit= new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(SmecGsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(SmecRailApi.base_url)
                .build();
    }

    public static SmecRailService getSmecRailService(){
        if(smecRailService == null){
            synchronized (syncObj){
                if(smecRailService == null){
                    smecRailService=mRetrofit.create(SmecRailService.class);
                }
            }
        }
        return smecRailService;
    }
}
